package com.test.typeGame;

import com.test.typeGame.core.TGConstant;
import com.test.typeGame.core.TGPanel;
import com.test.typeGame.core.TGVersion;
import com.test.typeGame.core.TGZhCharObject;

import java.awt.*;
import java.awt.event.*;

public class TypeGameApp {
    public static Frame frame = new Frame();
    public static TextField textField = new TextField("");
    public static TGPanel tgPanel = new TGPanel();
    public static int getFrameWidth(){
        return frame.getWidth();
    }
    public static int getFrameHeight(){
        return frame.getHeight();
    }
    public static void main(String []args){
        String gameName = "打字练习";
        String version = TGVersion.getVersion();

        frame.setTitle(gameName+version);
        frame.setSize(TGConstant.FRAME_WIDTH,TGConstant.FRAME_HEIGHT);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();

        frame.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);

        //打叉退出
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });


        frame.add(tgPanel);
        frame.addKeyListener(tgPanel);
        tgPanel.addKeyListener(tgPanel);
        frame.add(BorderLayout.SOUTH,textField);
        textField.setSize(500,100);
        textField.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent e){
                if (e.getKeyChar() == KeyEvent.VK_ENTER){
                    tgPanel.dealZhChar(textField.getText());
                    textField.setText(" ");
                }
                if (e.getKeyCode() == KeyEvent.VK_F3){
                    tgPanel.stop();
                }
                if (e.getKeyCode() == KeyEvent.VK_F1){
                    tgPanel.restart();
                }
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
                    System.exit(0);
                }
            }

            public void keyReleased(KeyEvent e){}
            public void keyTyped(KeyEvent e){}
        });

        Thread thread = new Thread(tgPanel);
        thread.start();
        frame.setVisible(true);
        tgPanel.requestFocus();
    }
}
